package com.luo.common.base;

import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

/**
 * rocketmq工具类.
 *
 * @author breggor
 */
@UtilityClass
public class RocketMQBuilder {

    private static final String KEYS = "KEYS";

    private static final String TAGS = "TAGS";

    /**
     * 构建消息.
     *
     * @param event 事件对象
     * @param <T>   事件类型
     * @return T
     */
    public <T extends BaseEvent> Message<T> buildMessage(final T event) {
        if (StringUtils.isBlank(event.keys()) || StringUtils.isBlank(event.tags())) {
            throw new RuntimeException("keys或tags是必填项");
        }
        return MessageBuilder.withPayload(event).setHeader(TAGS, event.tags()).setHeader(KEYS, event.keys()).build();
    }

}
